class_name MagicBulletBase
extends Node2D


@onready var hit_area: Area2D = $HitArea

var sender: Node2D
var attack: float = 0
var critical_rate: float = 0
var critical_bonus: float = 0
var knockback: float = 0
var affect_vertical_velocity: float = 0
var hit_cam_shake_duration: float = 0
var hit_cam_shake_strength: float = 0

var poison_hit_rate: float = 0
var poison_debuff_duration: float = 0
var poison_damage_per_second: float = 0

var hit_targets: Array = []
var is_enemy: bool = false


func set_is_enemy(b: bool):
	is_enemy = b
	if b:
		hit_area.set_collision_mask_value(7, true)
		hit_area.set_collision_mask_value(6, false)
	else:
		hit_area.set_collision_mask_value(7, false)
		hit_area.set_collision_mask_value(6, true)
		
	
func process_hit():
	var areas = hit_area.get_overlapping_areas()
	if areas.size() <= 0: return
	
	for area in areas:
		var source = area.source as Node2D
		if source.has_method("take_hit") and source not in hit_targets:
			hit_targets.append(source)
			source.take_hit(get_hit_data(), sender)
			
			if hit_cam_shake_strength:
				var level = get_tree().current_scene as Level
				level.camera.start_shake(hit_cam_shake_strength, hit_cam_shake_duration)
			
			if self.has_method("on_hit"):
				self.call("on_hit", source)


func get_hit_data():
	var critical_rate = critical_rate
	var is_critical = (randf() <= critical_rate)
	var critical_attack = attack * (1.0 + critical_bonus)
	
	var hit_data = HitData.new()
	hit_data.is_critical = is_critical
	hit_data.attack = attack if not is_critical else critical_attack
	hit_data.knockback = knockback
	hit_data.affect_vertical_velocity = affect_vertical_velocity
	
	return hit_data
	

func set_collision_2_ground(b: bool):
	if b:
		hit_area.set_collision_mask_value(1, true)
	else:
		hit_area.set_collision_mask_value(1, false)
